package com.tiny.cqzcp.utils;

import com.tiny.cqzcp.pojo.klb.Lottery;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import org.springframework.util.StringUtils;

/**
 * @author jmz
 */
public class ToolkitUtil {

    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 计算遗漏
     * @title: getMissValue
     * @Author: jmz
     * @DateTime: 2021/2/19 10:18
     **/
    public static Integer getMissValue(String term1, String term2,String tempTerm,List<Lottery> maxTerms){
        String term1date = "";
        String term2date = term2.substring(0, term2.length() - 3);
        int year2 = Integer.parseInt(term2date);
        int x2 = Integer.parseInt(term2.substring(term2.length() - 3));
        int maxX = 0;
        if(StringUtils.isEmpty(term1)){
            term1date = tempTerm.substring(0, tempTerm.length() - 3);
            int year1 = Integer.parseInt(term1date);
            for(int i=1;i<=(year2 - year1);i++){
                int x = Integer.parseInt(maxTerms.get(i).getLotteryNo().substring(term2.length() - 3));
                maxX += x;
            }
            return maxX + x2;
        }else{
            term1date = term1.substring(0, term1.length() - 3);
            int x1 = Integer.parseInt(term1.substring(term1.length() - 3));
            if(term2date.equals(term1date)){
                return x2 - x1;
            }else{
                int year1 = Integer.parseInt(term1date);
                for(int i=1;i<=(year2 - year1);i++){
                    int x = Integer.parseInt(maxTerms.get(i).getLotteryNo().substring(term2.length() - 3));
                    maxX += x;
                }
                return maxX - x1 + x2;
            }
        }
    }

    /**
     * 异常信息构建字符
     *
     * @param t
     * @return
     */
    public static String printStackTraceToString(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }

    /**
     * 获取当前日期
     * @title: currentDate
     * @Author: jmz
     * @DateTime: 2021/2/19 11:31
     **/
    public static String currentDate(){
        DateTimeFormatter fmDate = DateTimeFormatter.ofPattern(YYYY_MM_DD);
        LocalDate today = LocalDate.now();
        return today.format(fmDate);
    }

    /**
     * @Description 获取当前时间
     * @title currentDateTime
     * @date 2021/2/20
     * @auther Administrator
     */
    public static String currentDateTime(){
        DateTimeFormatter fmDate = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
        LocalDateTime now = LocalDateTime.now();
        return now.format(fmDate);
    }

    /**
     * @Description
     * @title cutList
     * @date 2021/3/10
     * @auther Administrator
     * @param number 份数
     * @param list 元素数据
     */
    public static  List<String> cutList(List<String> list, int number,int total){
        List<String> data = new ArrayList<>();
        int size = list.size() / total;
        for(int m=0;m<total;m++){
            List<String> tmp = list.subList(size*m,size*(m+1));
            StringJoiner joiner = new StringJoiner(",");
            for(int n=0;n<tmp.size();n++){
                joiner.add(tmp.get(n));
            }
            data.add(joiner.toString());
        }
        Collections.shuffle(data);
        String[] arr = new String[total];
        for(int i=0;i<total;i++){
            arr[i] = String.valueOf(i);
        }
        List<String> result = new ArrayList<>();
        CombineUtil combineUtil = new CombineUtil();
        ArrayList<String> arrayList = combineUtil.getResult(0,number,arr);
        for(int j=0;j<arrayList.size();j++){
            String s = arrayList.get(j).replace(" ","");
            s = s.replace("[","");
            s = s.replace("]","");
            StringJoiner stringJoiner = new StringJoiner(",");
            String[] indexArr = s.split(",");
            for(String index : indexArr){
                stringJoiner.add(data.get(Integer.parseInt(index)));
            }
            result.add(stringJoiner.toString());
        }
        return result;
    }

    /**
     * 获取定义长度的随机数的list
     * @title: randomList
     * @Author: jmz
     * @DateTime: 2021/3/25 17:28
     **/
    public static List<String> randomList(int len){
        List<String> newArray = new ArrayList<>();
        for(int i=1;i<len+1;i++){
            String n = String.valueOf(i);
            if(i < 10){
                n = "0" + n;
            }
            newArray.add(n);
        }
        Collections.shuffle(newArray);
        return newArray;
    }

}
